﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void BasicSearch(object sender, EventArgs e)
    {
        DoBasicSearch();
    }

    private void DoBasicSearch()
    {
        //";
        string tsp = GetStringKey();
        string qSelect = "Select * from SANPHAM where TenSanPham like '" + tsp + "'" + GetAdvanceQuery() + " order by NgayHetHan desc";
        SqlCommand cmd = new SqlCommand(qSelect, Helper.ConnectToDataBase());
        try
        {
            rptProducts.DataSource = cmd.ExecuteReader();
            rptProducts.DataBind();
            if (rptProducts.Items.Count == 0)
                lbStatus.Text = "Không tìm thấy kết quả.";
            else
                lbStatus.Text = "";
            Helper.DisposeCommand(ref cmd);
        }
        catch (Exception)
        {
            Helper.DisposeCommand(ref cmd);
        }
        panelSanPham.Visible = true;
    }

    private string GetAdvanceQuery()
    {
        string str = "";
        if (panelAdvanceSearch.Visible == true)
        {
            if (TryIntParse(txtGiaTu.Text))
                str += " and GiaKhoiDiem>=" + int.Parse(txtGiaTu.Text).ToString();

            if (TryIntParse(txtGiaDen.Text))
                str += " and GiaHienTai<=" + int.Parse(txtGiaDen.Text).ToString();

            if (TryDateTimeParse(txtNgayDang1.Text))
                str += " and DATEDIFF(day,NgayDang,'" + lbnd1.Text + "')<=0";
            if (TryDateTimeParse(txtNgayDang2.Text))
                str += " and DATEDIFF(day,NgayDang,'" + lbnd2.Text + "')>=0";

            if (TryDateTimeParse(txtNgayHetHan1.Text))
                str += " and DATEDIFF(day,NgayHetHan,'" + lbnhh1.Text + "')<=0";
            if (TryDateTimeParse(txtNgayHetHan2.Text))
                str += " and DATEDIFF(day,NgayHetHan,'" + lbnhh2.Text + "')>=0";

            if (DropDownListLoaiSanPham.SelectedIndex > 0)
                str += " and MaLoaiSanPham=" + DropDownListLoaiSanPham.SelectedIndex.ToString();

        }
        return str;
    }

    private bool TryIntParse(string value)
    {
        try
        {
            int.Parse(value);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

    private bool TryDateTimeParse(string value)
    {
        try
        {
            DateTime.Parse(value);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

    private string GetStringKey()
    {
        string key = txtTenSanPham.Text;
        key = "%" + key + "%";
        key = key.Replace(' ', '%');
        return key;
    }

    protected void Toogle(object sender, EventArgs e)
    {
        SetToogle(!panelAdvanceSearch.Visible);
    }

    private void SetToogle(bool b)
    {
        panelAdvanceSearch.Visible = b;
        if (b == true)
        {
            btToggle.Text = "Tìm kiếm cơ bản";
            InitAdvanceSearch();
        }
        else
            btToggle.Text = "Tìm kiếm nâng cao";
    }

    private void InitAdvanceSearch()
    {
        LoadLoaiSanPham();
        //calendarSearch.SelectedDay = DateTime.MinValue;
        calendarSearch.Visible = false;
    }

    private void LoadLoaiSanPham()
    {
        DropDownListLoaiSanPham.SelectedIndex = -1;
        if (DropDownListLoaiSanPham.Items.Count > 2)
            return;
        string query = "select MaLoaiSanPham, TenLoaiSanPham from LOAISANPHAM";
        SqlCommand cmd = new SqlCommand(query, Helper.ConnectToDataBase());
        DropDownListLoaiSanPham.DataSource = cmd.ExecuteReader();
        DropDownListLoaiSanPham.DataTextField = "TenLoaiSanPham";
        DropDownListLoaiSanPham.DataValueField = "MaLoaiSanPham";
        DropDownListLoaiSanPham.DataBind();
        Helper.DisposeCommand(ref cmd);
    }

    protected void AdvanceSearch(object sender, EventArgs e)
    {
    }

    protected void ShowCalNgayDang1(object sender, EventArgs e)
    {
        lbTxtFlag.Text = "11";
        calendarSearch.Visible = true;
    }

    protected void ShowCalNgayDang2(object sender, EventArgs e)
    {
        lbTxtFlag.Text = "12";
        calendarSearch.Visible = true;
    }

    protected void ShowCalHetHan1(object sender, EventArgs e)
    {
        lbTxtFlag.Text = "21";
        calendarSearch.Visible = true;
    }

    protected void ShowCalHetHan2(object sender, EventArgs e)
    {
        lbTxtFlag.Text = "22";
        calendarSearch.Visible = true;
    }

    protected void ResetSearchForm(object sender, EventArgs e)
    {
        lbTxtFlag.Text = "";
        txtGiaTu.Text = "";
        txtGiaDen.Text = "";
        txtNgayDang1.Text = "";
        txtNgayDang2.Text = "";
        txtNgayHetHan1.Text = "";
        txtNgayHetHan2.Text = "";
        calendarSearch.Visible = false;
    }


    protected void CalendarSelection(object sender, EventArgs e)
    {
        switch (lbTxtFlag.Text)
        {
            case "11":
                txtNgayDang1.Text = calendarSearch.SelectedDate.ToString("dd/MM/yyyy");
                lbnd1.Text = calendarSearch.SelectedDate.ToString("s");
                break;
            case "12":
                txtNgayDang2.Text = calendarSearch.SelectedDate.ToString("dd/MM/yyyy");
                lbnd2.Text = calendarSearch.SelectedDate.ToString("s");
                break;
            case "21":
                txtNgayHetHan1.Text = calendarSearch.SelectedDate.ToString("dd/MM/yyyy");
                lbnhh1.Text = calendarSearch.SelectedDate.ToString("s");
                break;
            case "22":
                txtNgayHetHan2.Text = calendarSearch.SelectedDate.ToString("dd/MM/yyyy");
                lbnhh2.Text = calendarSearch.SelectedDate.ToString("s");
                break;
        }
        calendarSearch.Visible = false;
    }
}
